package com.zhumo.sz.table.hive;

import java.util.Map;
import java.util.Objects;
import java.util.Properties;

/**
 * @author jiaqi.zhu
 * @comment hive ddl 语句 常用配置,
 * eg:
 * create table if not exists table (col1 string,col2 int)
 * stored as testfile
 * row format delimited fields teminated by '/t';
 * -- STORED AS ORC
 * -- TBLPROPERTIES ("orc.compress"="ZLIB");
 * @time  2024-04-03
 */
public class HiveDDLConfig {

    /**
     * 数据间隔符
     */
    public static final String ROW_FORMAT = " ROW FORMAT DELIMITED FIELDS TERMINATED BY ";

    /**
     * 存储格式
     */
    public static final String STORE_FORMAT =" STORED AS ";

    /**
     * 压缩格式
     */
//    private static final String TRAN_FORMAT ="";

    /**
     * 分区格式
     */
    public static final String PARTITION_FORMAT = " PARTITIONED BY ";

    /**
     * 存储位置
     */
    public static final String LOCATION_FORMAT =" LOCATION ";

    /**
     * 外部表
     */
    public static final String EXTERNAL_TABLE = " EXTERNAL ";

    /**
     * 注释
     */
    public static final String COMMENT = " COMMENT ";

    /**
     * tblproperties("key1"="v1","key2"="v2")
     */
    public static String makeTableProperties(Properties properties){
        if (Objects.isNull(properties) || properties.size() <=0){
            return "";
        }
        StringBuilder tblpropertiesBuilder = new StringBuilder("tblproperties");
        tblpropertiesBuilder.append("(");
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            String key = "\"" + entry.getKey() +"\"";
            String value = "\"" + entry.getValue() + "\"";
            tblpropertiesBuilder.append(key).append("=").append(value).append(",");
        }
        tblpropertiesBuilder.delete(tblpropertiesBuilder.length()-1,tblpropertiesBuilder.length());
        tblpropertiesBuilder.append(")");
        return tblpropertiesBuilder.toString();
    }
}
